public class BluehouseService extends AtomBase {

	private Map<String, Workspace> workspaceMap = new Map<String, Workspace>();
	
	public String generator { get; set; }
	public String generatorUri { get; set; }
	public String version { get; set; }
	public Map<String, String> namespaces { 
		get {
			if (namespaces == null) {
				namespaces = new Map<String, String>();
			}
			return namespaces;
		}
		 set; 
	}
	
	public List<Workspace> workspaces { 
		get {
			if (workspaces == null) {
				workspaces = new List<Workspace>();
			}
			return workspaces;
		} 
		set; 
	} 
	
	public List<ServiceCollection> getActivitiesCollection() {
		return getActivities().collection;
	}
	
	public Integer getWorkspaceCount() {
		return workspaces.size();
	}
	
	public List<String> getWorkspaceNames() {
		List<String> names = new List<String>();
		for (Workspace w : workspaces) {
			names.add(w.getTitle());
		}
		return names;
	}
	
	public Workspace getWorkspaceByContains(String title) {
		for (String key : workspaceMap.keySet()) {
			if (key.indexOf(title) > -1) {
				return workspaceMap.get(key);
			}
		}
		return null;
	}
	
	public Workspace getWorkspaceByTitle(String title) {
		return workspaceMap.get(title);
	}

	public Workspace getWorkspaceByTitleStartsWith(String title) {
		for (String s : workspaceMap.keySet()) {
			if (s.startsWith(title)) {
				return workspaceMap.get(s);
			}
		}
		return null;
	}
	
	public ServiceCollection getOverview() {
		Workspace ws = getWorkspaceByTitle('Activity');
		if (ws != null) {
			return ws.getCollectionByTitle('Overview');
		} else {
			return null;
		}
	}
	
	private Workspace getActivities() {
		return getWorkspaceByTitleStartsWith('Activities');
	}
		
	public String getActiviesPath() {
		ServiceCollection sc = getOverView();
		if (sc != null) {
			return sc.href;
		} else {
			return null;
		}
	}
	
	public String getActivitiesUri() {
		ServiceCollection sc = getCollection('Activity', 'Overview');
		if (sc != null) {
			return sc.href;
		} else {
			return null;
		}
	}
	
	public String getCompletedActivitiesUri() {
		ServiceCollection sc = getCollection('Activity', 'Completed');
		if (sc != null) {
			return sc.href;
		} else {
			return null;
		}
	}
	
	public String getTunedOutActivitiesUri() {
		ServiceCollection sc = getCollection('Activity', 'Tuned out');
		if (sc != null) {
			return sc.href;
		} else {
			return null;
		}
	}

	public String getDeletedActivitiesUri() {
		ServiceCollection sc = getCollection('Activity', 'Trash');
		if (sc != null) {
			return sc.href;
		} else {
			return null;
		}
	}

	public String getPublicActivitiesUri() {
		ServiceCollection sc = getCollection('Activity', 'Public');
		if (sc != null) {
			return sc.href;
		} else {
			return null;
		}
	}

	public String getEverythingUri() {
		ServiceCollection sc = getCollection('Activity', 'Everything');
		if (sc != null) {
			return sc.href;
		} else {
			return null;
		}
	}

	public String getToDosUri() {
		ServiceCollection sc = getCollection('Activity', 'To Do List');
		if (sc != null) {
			return sc.href;
		} else {
			return null;
		}
	}
 
	public ServiceCollection getCollection(String workspaceName, String collectionTitle) {
		ServiceCollection sc;
		Workspace ws = getWorkspaceByTitle(workspaceName);
		if (ws != null) {
			sc = ws.getCollectionByTitle(collectionTitle);
		}
		return sc;
	}
	 
	public List<ServiceCollection> getAllCollections() {
		List<ServiceCollection> c = new List<ServiceCollection>();
		for (Workspace w : workspaces) {
			for (ServiceCollection sc : w.collection) {
				c.add(sc);
			}
		}
		return c;
	}
	
	private void createWorkspaceMap() {
		for (Workspace w : workspaces) {
			workspaceMap.put(w.getTitle(), w);
		}
	}
	
	public BluehouseService(XmlStreamReader reader) {
		super(reader);
		return;
		namespaces = new Map<String, String>();
		handleAttributes(reader, 'service');
        if (reader.hasNext()) {
			reader.next();
			String tagname;
			while (reader.hasNext()) {
				if (reader.getEventType() == XmlTag.END_ELEMENT) {
					if (reader.getLocalName() == 'service') {
						return;
					} 
				} else if (reader.getEventType() == XmlTag.CHARACTERS) {
                	if ( ! reader.isWhitespace()) { 
                		if (tagName == 'generator') {
                			generator = reader.getText();
                		}
                	}
            	} else if (reader.getEventType() == XmlTag.START_ELEMENT) {
            		tagName = reader.getLocalName(); 
            		System.debug(LoggingLevel.INFO, '\n\n BLUEHOUSE SERVICE - start tag: ' + tagName + '\n');
					handleAttributes(reader, tagName);
					if (tagName == 'workspace') {
						workspaces.add(new Workspace(reader));
					}
				}
               	reader.next(); 
			}
        }
        createWorkspaceMap();
	}
	
	public virtual override void handleStartTag(XmlStreamReader reader, String nodeName) {
		System.debug(LoggingLevel.INFO, '\n\nHandling the start tag in the BluehouseService...\n');
   		System.debug(LoggingLevel.INFO, '\n\n BLUEHOUSE SERVICE - start tag: ' + nodeName + '\n');
			while (reader.hasNext()) {
				if (reader.getEventType() == XmlTag.END_ELEMENT) {
					if (reader.getLocalName() == 'service') {
						return;
					} 
				} else if (reader.getEventType() == XmlTag.CHARACTERS) {
                	if ( ! reader.isWhitespace()) { 
                		if (nodeName == 'generator') {
                			generator = reader.getText();
                		}
                	}
            	} else if (reader.getEventType() == XmlTag.START_ELEMENT) {
            		nodeName = reader.getLocalName(); 
            		System.debug(LoggingLevel.INFO, '\n\n BLUEHOUSE SERVICE - start tag: ' + nodeName + '\n');
					handleAttributes(reader, nodeName);
					if (nodeName == 'workspace') {
						workspaces.add(new Workspace(reader));
	            		System.debug(LoggingLevel.INFO, '\n\n BLUEHOUSE SERVICE - WORKSPACE ADDED\n ' + reader.getLocalName() + '\n');
					}
				}
               	reader.next(); 
			}
	}
	
	private void handleAttributes(XmlStreamReader reader, String tagName) {
		for (integer i=0; i<reader.getAttributeCount(); i++) {
       		String name = reader.getAttributeLocalName(i);
       		String value = reader.getAttributeValueAt(i);
        	if (tagName == 'generator') {
        		if (name == 'uri') {
        			generatorUri = value;
        		} else if (name == 'version') {
        			version = value;
        		}
        	} else if (tagName == 'service') {
        		if (name.startsWith('xmlns')) {
        			namespaces.put(name, value);
        		}
        	}
        }
       
	}
	
}